# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=grommunio-web
pkgdesc="Web user interface for grommunio"
pkgver=3.7
pkgrel=0
arch="noarch !riscv64"
url="http://www.grommunio.com"
license="AGPL-3.0-only"
options="!check" # No test suite
_php=php83
install="$pkgname.pre-install"
depends="grommunio-gromox
	grommunio-mapi-header-php
	$_php
	$_php-gettext
	$_php-bcmath
	$_php-ctype
	$_php-curl
	$_php-dom
	$_php-gd
	$_php-gettext
	$_php-iconv
	$_php-mbstring
	$_php-openssl
	$_php-sodium
	$_php-sqlite3
	$_php-sysvshm
	$_php-zip
	"

makedepends="libxml2-utils $_php-gettext npm gettext"

source="https://github.com/grommunio/grommunio-web/archive/refs/tags/grommunio-web-$pkgver.tar.gz
	0001-makefile.patch
	0002-config-php-dist.patch
	0003-plugins-files-config-php.patch
	"

builddir="$srcdir/grommunio-web-grommunio-web-$pkgver"

build() {
	make -j1
}

package() {
	# webapp
	install -dm0755 "$pkgdir/usr/share/webapps/"
	cp -rp deploy "$pkgdir/usr/share/webapps/$pkgname"

	# license
	install -Dm644 LICENSE.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

	# nginx conf
	sed -i "s@/var/lib/grommunio-web@/var/tmp/grommunio-web@" build/grommunio-web.conf
	install -Dpm644 build/grommunio-web.conf "$pkgdir/usr/share/grommunio-common/nginx/locations.d/grommunio-web.conf"
	install -Dpm644 build/grommunio-web-upstream.conf "$pkgdir/usr/share/grommunio-common/nginx/upstreams.d/grommunio-web.conf"

	# php-fpm
	install -Dpm644 build/pool-grommunio-web.conf "$pkgdir/etc/$_php/php-fpm.d/pool-grommunio-web.conf"

	# web config
	install -Dm644 config.php.dist "$pkgdir/etc/grommunio-web/config.php"
	ln -sf "/etc/grommunio-web/config.php" "$pkgdir/usr/share/webapps/grommunio-web/config.php"
		rm -f "$pkgdir/usr/share/webapps/grommunio-web/config.php.dist"
	rm -f "$pkgdir/usr/share/webapps/grommunio-web/debug.php.dist"

	# plugin config
	for dir in "$pkgdir/usr/share/webapps/grommunio-web/plugins"/*; do
		plugindir=$(basename "$dir")
		if [ -f "$pkgdir/usr/share/webapps/grommunio-web/plugins/$plugindir/config.php" ]; then
			mv "$pkgdir/usr/share/webapps/grommunio-web/plugins/$plugindir/config.php" "$pkgdir/etc/grommunio-web/config-$plugindir.php"
			ln -s "/etc/grommunio-web/config-$plugindir.php" "$pkgdir/usr/share/webapps/grommunio-web/plugins/$plugindir/config.php"
		fi
	done

	# npm gives ownership of ALL FILES to build user
	# https://bugs.archlinux.org/task/63396
	chown -R root:root "$pkgdir"

}

sha512sums="
4bd72cbfbe7dd156c8a8a6b4f58e8c78c1fe457eb5b90a159a56613e209f7fdde5c2a7dd315c8375f5030a938257602d1b9cb503df521a8f30956a343fc14d73  grommunio-web-3.7.tar.gz
fcfb070ab0bb8cca6f24221fe66eeac7e0c33981c16fff947db7509cc73d39b086710e5fd75bce34a230fa18bc10828d87410d98d147d31cc6ae1c16c41b48ae  0001-makefile.patch
aaf924c834454078e512a7dff44fca647416eabb328e90e7cc1d78873c2ecb8f5f4a139284f8a4a4c9119ea8aec61d34d343ec1e8bced77f3dfbcdabae32d0d7  0002-config-php-dist.patch
2e3f55bc799c712ecf453cacd9da74893b07f8cf9ec1b6954993ae15a229219c612b632b372f9bc69ae6f8eb010c65484b7b55c3e90af3a0049c525321ba606e  0003-plugins-files-config-php.patch
"
